package com.bw.sparksql1.job2

import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}
import org.apache.spark.sql.{DataFrame, Row, SparkSession}

/**
  *
  *  RDD -> DataFrame 案例2
  */

object Job7 {
    def main(args: Array[String]): Unit = {
      Logger.getLogger("org").setLevel(Level.ERROR)
      val spark = SparkSession
        .builder()
        .master("local")
        .appName("Spark SQL basic example")
        .getOrCreate()

      val rdd1 = spark.sparkContext.textFile("person4.txt")
      //导入隐式转换
      import spark.implicits._
      //绑定数据到Row对象
      val rddRow = rdd1.map(line => {
        val fields = line.split(",")
        Row(fields(0),fields(1))
      })
      rddRow.foreach(println(_))

      //创建dataframe的数据结构
      val structType = StructType(
        StructField("name",StringType,true) ::
          StructField("age",IntegerType,true) :: Nil
      )

      //生产dataFrame
      val dataFrame = spark.createDataFrame(rddRow,structType)
      dataFrame.createOrReplaceTempView("person")
      spark.sql("select count(*) from person").show()
    }
}